package com.graduation.mathematics.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.graduation.mathematics.R;
import com.graduation.mathematics.bean.ExerciseStatistics;
import com.graduation.mathematics.bean.Ranking;

import java.util.List;

public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.RankingViewHolder> {

    private List<ExerciseStatistics> mData;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public ExerciseAdapter(List<ExerciseStatistics> data, Context context) {
        mData = data;
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @NonNull
    @Override
    public RankingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RankingViewHolder(mLayoutInflater.inflate(R.layout.layout_item_exercise, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RankingViewHolder holder, int position) {
        ExerciseStatistics exerciseStatistics = mData.get(position);
        if (exerciseStatistics == null) {
            return;
        }
        holder.tvNum.setText(String.valueOf(exerciseStatistics.getNum()));
        holder.progressBar.setProgress(exerciseStatistics.getRightNum() / exerciseStatistics.getStudentCount());
        holder.tvPercent.setText(exerciseStatistics.getRightNum() + "/" + exerciseStatistics.getStudentCount());
    }

    @Override
    public int getItemCount() {
        return  mData.isEmpty() ? 0 : mData.size();
    }

    public void updateData(List<ExerciseStatistics> list){
        if (list != null && list.size() > 0){
            mData = list;
            notifyDataSetChanged();
        }
    }

    class RankingViewHolder extends RecyclerView.ViewHolder {
        TextView tvNum;
        ProgressBar progressBar;
        TextView tvPercent;

        RankingViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNum = itemView.findViewById(R.id.tv_num);
            progressBar = itemView.findViewById(R.id.item_progressbar);
            tvPercent = itemView.findViewById(R.id.item_percent);
        }
    }

}
